Skip to content

fix: add input validation across model_hub, conversions and alpha#216

Open
crazywriter1 wants to merge 2 commits intoOpenGradient:mainfrom
crazywriter1:fix/input-validation-v2
Open

fix: add input validation across model_hub, conversions and alpha#216
crazywriter1 wants to merge 2 commits intoOpenGradient:mainfrom
crazywriter1:fix/input-validation-v2

Conversation

@crazywriter1
Copy link
Copy Markdown

Summary

  • model_hub.py — Guard response.json() in upload() against HTML error pages: when the server returns a non-JSON response (e.g. a WAF or proxy error page), the previous code crashed with JSONDecodeError; now falls back to response.text. Note: create_model's successful-response response.json() call (HTTP 200 path) is intentionally out of scope — a 200 response returning HTML is not a realistic failure scenario.
  • model_hub.py — Replace bare Exception with RuntimeError in create_model and create_version across all raise paths; fix create_version docstring which still claimed Raises: Exception; remove redundant except Exception: raise block that served no purpose
  • _conversions.py — Add type guard at the top of convert_to_model_output: if event_data is None or not dict-like, raises TypeError with a clear message instead of an unhelpful AttributeError mid-function
  • alpha.py — Validate inference_contract_address in the Alpha constructor using Web3.is_address() so invalid addresses fail immediately at construction time with a clear ValueError instead of failing silently during a transaction

Files Changed

  • src/opengradient/client/model_hub.py
  • src/opengradient/client/_conversions.py
  • src/opengradient/client/alpha.py
  • tests/client_test.py

Test Plan

  • Full test suite passes: uv run pytest tests/ -v122 passed
  • TestModelHubUploadErrorHandling — verifies HTML response raises RuntimeError, JSON error response raises RuntimeError with detail message
  • TestConvertToModelOutputGuard — verifies None and non-dict inputs raise TypeError, empty dict returns empty output
  • TestAlphaAddressValidation — verifies invalid address raises ValueError at construction time, valid address passes, empty string raises ValueError; Web3.is_address is called before address assignment (constructor-level validation, not checksum rule testing)

- Guard response.json() in model_hub.py upload() against HTML error pages
- Replace bare Exception with RuntimeError in model_hub.py create_model and create_version
- Fix create_version docstring and remove redundant except Exception: raise block
- Add type guard for event_data in _conversions.py convert_to_model_output
- Validate inference_contract_address format in Alpha constructor

Signed-off-by: crazywriter1 <crazywriter1@users.noreply.github.com>
@crazywriter1
Copy link
Copy Markdown
Author

crazywriter1 commented Mar 30, 2026

Can you check this? @adambalogh

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant